﻿#region Usings
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using InsuranceAdjuster;
using InsuranceAdjuster.Helpers;
using InsuranceAdjuster.OccasionallyConnected;
using InsuranceAdjuster.OccasionallyConnected.Messages;
using InsuranceAdjuster.Services;
using InsuranceAdjuster.Web.Models;
using Papa.Common;
#endregion

namespace InsuranceAdjuster.ViewModels
{
    public class DamageViewModel : ViewModelBase<ClaimDamage>
    {

        public RelayCommand AddPhotosCommand { get; set; }
        public RelayCommand<String> AddUsbPhotosCommand { get; set; }
        public RelayCommand<ClaimDamage> DeleteClaimDamageCommand { get; set; }
        public RelayCommand<ClaimDamage> SelectClaimDamageCommand { get; set; }

        public DamageViewModel(IPageConductor pageConductor)
        {
            PageConductor = pageConductor;
            RegisterCommands();
        }

        protected override void RegisterCommands()
        {
            //AddPhotosCommand = new RelayCommand(OnAddPhotos);
            //AddUsbPhotosCommand = new RelayCommand<String>(OnAddUsbPhotos);
            //DeleteClaimDamageCommand = new RelayCommand<ClaimDamage>(OnDeleteClaimDamage, (x) => _selectedClaimDamage != null);
            //SelectClaimDamageCommand = new RelayCommand<ClaimDamage>(OnSelectClaimDamage);
            //AddPhotosCommand = new RelayCommand( () => new OpenFileDialog().ShowDialog());

        }
        protected override void OnNewModelAvailable(ClaimDamage currentModel, ClaimDamage newerModel)
        {
        }
        protected override void OnEntityChanged(Guid entityGuid, Type entityType, ChangeOperation changeOperation, Origin changeOrigin, IEntity entity)
        {
        }

        #region Photo Management
        //private void OnAddUsbPhotos(string path)
        //{
        //    var filePaths = Directory.EnumerateFiles(path);
        //    foreach (var ThisFile in filePaths)
        //    {
        //        if (Path.GetExtension(ThisFile).ToLower() == ".jpg")
        //        {
        //            using (var MyStream = File.OpenRead(ThisFile))
        //            {
        //                var NewDamage = new ClaimDamage();
        //                this.SelectedClaim.ClaimDamages.Add(NewDamage);
        //                this.SelectedClaimDamage = NewDamage;

        //                var MyBuffer = new Byte[MyStream.Length];
        //                MyStream.Read(MyBuffer, 0, MyBuffer.Length);
        //                NewDamage.Image = MyBuffer;
        //            }
        //        }
        //    }
        //}

        //private void OnAddPhotos()
        //{
        //    var fileDialog = new OpenFileDialog();
        //    fileDialog.Filter = "JPEGs|*.jpg";
        //    fileDialog.Multiselect = true;
        //    if (true == fileDialog.ShowDialog())
        //    {
        //        foreach (var item in fileDialog.Files)
        //        {
        //            using (var fileStream = item.OpenRead())
        //            {
        //                var claimDamage = new ClaimDamage();
        //                this.SelectedClaim.ClaimDamages.Add(claimDamage);
        //                this.SelectedClaimDamage = claimDamage;

        //                var fileBuffer = new Byte[fileStream.Length];
        //                fileStream.Read(fileBuffer, 0, fileBuffer.Length);
        //                claimDamage.Image = fileBuffer;
        //            }
        //        }
        //    }
        //}
        //public Visibility DamagePhotoPromptVisibility
        //{
        //    get
        //    {
        //        if (this.SelectedClaim == null || SelectedClaim.ClaimDamages.Count == 0)
        //            return Visibility.Visible;
        //        else
        //            return Visibility.Collapsed;
        //    }
        //}

        //public Visibility DamagePhotosVisibility
        //{
        //    get
        //    {
        //        if (SelectedClaim == null || SelectedClaim.ClaimDamages.Count == 0)
        //            return Visibility.Collapsed;
        //        else
        //            return Visibility.Visible;
        //    }
        //}
        //public Visibility DamagePhotoVisibility
        //{
        //    get
        //    {
        //        if (_selectedClaimDamage == null)
        //            return Visibility.Collapsed;
        //        else
        //            return Visibility.Visible;
        //    }
        //}
        #endregion


        //private ClaimDamage _selectedClaimDamage;
        //public bool IsDamageItemSelected
        //{
        //    get
        //    {
        //        if (_selectedClaimDamage == null)
        //            return false;
        //        else
        //            return true;
        //    }
        //}
        //public ClaimDamage SelectedClaimDamage
        //{
        //    get { return _selectedClaimDamage; }
        //    set
        //    {
        //        if (_selectedClaimDamage == value)
        //            return;

        //        if (_selectedClaimDamage != null)
        //            _selectedClaimDamage.PropertyChanged -= OnClaimDamagePropertyChanged;

        //        _selectedClaimDamage = value;

        //        if (_selectedClaimDamage != null)
        //            _selectedClaimDamage.PropertyChanged += OnClaimDamagePropertyChanged;

        //        RaisePropertyChanged("SelectedClaimDamage");
        //        RaisePropertyChanged("IsDamageItemSelected");
        //        RaisePropertyChanged("DamagePhotoVisibility");
        //    }
        //}


        void OnClaimPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            RaisePropertyChanged("DamagePhotoPromptVisibility");
            RaisePropertyChanged("DamagePhotosVisibility");
        }
        void OnClaimDamagePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            RaisePropertyChanged("DamagePhotoPromptVisibility");
            RaisePropertyChanged("DamagePhotosVisibility");
        }
    }
}
